package com.example.teamtribe;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.common.model.UserModel;
import com.example.teamtribe.database.MegId;
import com.example.teamtribe.database.Request;
import com.example.teamtribe.database.Team;
import com.example.teamtribe.database.TeamRequest;
import com.example.teamtribe.database.User;
import com.example.teamtribe.event.Friends;
import com.example.teamtribe.main.club.ClubModel;
import com.example.teamtribe.main.club.manage.feedback.apply.AppListModel;
import com.example.teamtribe.utils.Utility;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class UpdateService extends Service {

    private Binder binder;
    private UserModel model;

    public UpdateService() {
        binder = new Binder();
    }

    @Override
    public IBinder onBind(Intent intent) {
        model = UserModel.getInstance();
        return binder;
    }

    public class Binder extends android.os.Binder {

        public void getFriends() {
            model.getFriends(Utility.getToken(), new Back() {
                @Override
                public void success(@Nullable Bundle bundle) {
                    final ArrayList<Integer> friends = bundle.getIntegerArrayList("friends");
                    if (!friends.isEmpty()) {
                        for (int i : friends) {
                            getUser(i);
                        }
                    }

                    List<Integer> f = new ArrayList<>();
                    for (int i = 100001; i < 100017; i++) {
                        f.add(i);
                    }
                    friends.addAll(f);

                    User temp = Utility.getUser();
                    temp.setFriends(friends);
                    Utility.setUser(temp);

                    EventBus.getDefault().post(new Friends(friends));
                }

                @Override
                public void failure(String meg) {

                }
            });
        }

        private void getRelationBox(int type) {
            final String token = Utility.getToken();
            model.getMessages(token, type, new Back() {
                @Override
                public void success(@Nullable Bundle bundle) {
                    final List<MegId> megIds = bundle.getParcelableArrayList("messages");
                    if (megIds != null && !megIds.isEmpty()) {
                        for (final MegId megId : megIds) {
                            getRequest(token, megId.getId());
                        }
                    } else {
                        EventBus.getDefault().post(new Request());
                    }
                }

                @Override
                public void failure(String meg) {

                }
            });
        }

        private void getRequest(String token, int id) {
            model.getRequest(token, id, new Back() {
                @Override
                public void success(@Nullable Bundle bundle) {
                    Request request = bundle.getParcelable("request");
                    int userId = Utility.getUser().getRealId();
                    int id = userId == request.getId1() ? request.getId2() : request.getId1();
                    request.setUserId(id);
                    request.saveOrUpdate();
                    User user = new User();
                    user.setRealId(id);
                    user.saveOrUpdate();
                    getUser(id);
                    EventBus.getDefault().post(request);
                }

                @Override
                public void failure(String meg) {

                }
            });
        }

        private void getUser(int id) {
            new User(id).saveOrUpdate();
            model.getUser(Utility.getToken(), id, new FileModel.FileBack() {
                @Override
                public void loadSuccess(String fileName) {

                }

                @Override
                public void success(@Nullable Bundle bundle) {
                    User user = bundle.getParcelable("user");
                    user.saveOrUpdate();
                }

                @Override
                public void failure(String meg) {

                }
            });
        }

        private void getTeamResponseInfo(int id) {
            AppListModel.getInstance().getRequests(Utility.getToken(), id, new Back() {
                @Override
                public void success(@Nullable Bundle bundle) {
                    TeamRequest request = bundle.getParcelable("request");
                    request.saveOrUpdate();
                    new ClubModel().getTeam(request.getTeamId(), new FileModel.FileBack() {
                        private Team team;

                        @Override
                        public void loadSuccess(String fileName) {
                            team.saveOrUpdate();
                            EventBus.getDefault().post(new Request());
                        }

                        @Override
                        public void success(@Nullable Bundle bundle) {
                            team = bundle.getParcelable("team");
                        }

                        @Override
                        public void failure(String meg) {

                        }
                    });
                }

                @Override
                public void failure(String meg) {

                }
            });
        }

        public void getTeamResponse() {
            UserModel.getInstance().getMessages(Utility.getToken(), 4, new Back() {
                @Override
                public void success(@Nullable Bundle bundle) {
                    ArrayList<MegId> ids = bundle.getParcelableArrayList("messages");
                    if (ids == null || ids.isEmpty()) {
                        EventBus.getDefault().post(new Request());
                        return;
                    }
                    for (MegId megId : ids) {
                        getTeamResponseInfo(megId.getId());
                    }
                }

                @Override
                public void failure(String meg) {

                }
            });
        }

        public void getRequestMeg() {
            getRelationBox(1);
        }

        public void getResponseMeg() {
            getRelationBox(2);
        }
    }
}
